<template>
  <el-card class="page-container">
    <template #header>
      <div class="header">
        <span>教师信息管理</span>
      </div>
    </template>
    <el-form :model="teacherModel" :rules="rules" label-width="100px">
      <el-form-item label="教师姓名" prop="teacherName">
        <el-input v-model="teacherModel.teacherName"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="updateTeacher">修改</el-button>
      </el-form-item>
    </el-form>
  </el-card>
</template>

<script setup>
import { ref, onMounted } from 'vue';
import { ElMessage } from 'element-plus';
import { improveTeacherInfo, updateTeacherInfo } from '@/api/teacherInfoService.js';
import { useTokenStore } from '@/stores/token.js'; // 引入 useTokenStore

const tokenStore = useTokenStore(); // 获取 tokenStore 实例

const teacherModel = ref({
  teacherName: ''
});

const rules = {
  teacherName: [{ required: true, message: '请输入教师姓名', trigger: 'blur' }]
};

const updateTeacher = async () => {
  try {
    const result = await updateTeacherInfo(teacherModel.value); // 传递数据
    if (result.code === 0) {
      ElMessage.success(result.msg || '修改成功');
    } else {
      ElMessage.error(result.msg || '修改失败');
    }
  } catch (error) {
    if (error.message.includes('Unauthorized')) {
      ElMessage.error('未授权访问，请重新登录');
    } else {
      ElMessage.error('修改教师信息失败');
    }
  }
};

</script>

<style scoped>
.page-container {
  min-height: 100%;
  box-sizing: border-box;

  .header {
    display: flex;
    align-items: center;
    justify-content: space-between;
  }
}
</style>
